<template>
  <div>
    <!-- <h1>人气推荐详情{{ id }}</h1> -->

    <div class="detail-info">
      <h6>{{ basicInfo.name }}</h6>
      <p>{{ basicInfo.characteristic }}</p>
      <div>
        <div>
          <span>低价{{ basicInfo.minPrice }}</span>
          <span>原价{{ basicInfo.originalPrice }}</span>
        </div>
        <span> 库存{{ basicInfo.stores }} </span>
      </div>
    </div>
    <!-- 商品的基本信息 -->

    <ul class="detail-nav">
      <li :class="{ active: flag == 0 }" class="center" @click="flag = 0">
        商品介绍
      </li>
      <li :class="{ active: flag == 1 }" class="center" @click="flag = 1">
        商品评价
      </li>
    </ul>
    <!-- tab切换 -->

    <div v-html="content" v-if="flag == 0" class="content"></div>
    <!-- 商品介绍 -->
    <div v-else></div>
    <!-- 商品评价 要靠大家自己渲染数据 -->

    <div class="detail-footer">
      <div>
        <van-icon name="service-o"></van-icon>
      </div>
      <div>
        <van-icon
          name="service-o"
          :badge="$store.getters.allNum"
          @click="$router.push('/shopcar')"
        ></van-icon>
      </div>
      <div>
        <van-icon name="service-o"></van-icon>
      </div>
      <p>立即购买</p>
      <p @click="showPopup = true">加入购物车</p>
    </div>
    <!-- 详情页的底部 -->

    <van-popup
      v-model:show="showPopup"
      position="bottom"
      closeable
      class="popup"
    >
      <div>
        <h1>最终价格：{{ actPrice }}</h1>
        <ul class="pop-pro">
          <li v-for="(item, index) in properties" :key="item.id">
            <h5>{{ item.name }}</h5>
            <div>
              <span
                class="center"
                v-for="i in item.childsCurGoods"
                :key="i.id"
                @click="selectHandler(index, i)"
                :class="{ active: selectProperty.includes(i.name) }"
                >{{ i.name }}</span
              >
              <!-- :class="{ active: selectProperty.includes(i.name) }" 判断样式 如果选择规格的数组中包含 i.name 那么 这个span就会加上active的样式 -->
            </div>
          </li>
        </ul>
        <!-- 规格 -->
        <div class="sel-num">
          <span>购买数量</span>
          <van-stepper v-model="shopcarNum" />
        </div>
        <!-- 选择数量 -->
        <div class="add-shopcar center" @click="addShopcar">加入购物车</div>
      </div>
    </van-popup>

    <!-- 详情页的规格选择弹出框 -->
  </div>
</template>
<script>
import { useRoute } from "vue-router";
//路由信息对象
import { getGoodDetail, getActPrice } from "@/utils/http/home";
//获取商品详情的接口方法 获取确切价格
import { reactive, toRefs } from "vue";
//组合api的方法
import { useStore } from "vuex";
export default {
  setup(props) {
    const route = useRoute();
    //相当于是之前的this.$route.
    // const id = route.query.id;
    const id = route.params.id;
    //动态传参用params接收

    const store = useStore();
    //vuex仓库

    const data = reactive({
      basicInfo: {},
      //保存商品基本信息
      bannerList: [],
      //轮播图
      content: "",
      // 图文介绍
      properties: [],
      //保存商品规格
      flag: 0,
      //状态代表tab切换是在 商品介绍还是在评价里
      showPopup: false,
      //弹框的显示隐藏

      selectProperty: [],
      //选中的规格 存在这个数组中 数组的第一位代表尺寸 第二位代表颜色
      selectProIds: [],
      //选中的规格的id
      actPrice: "0.02",
      //选择规格后的确切的价格
      shopcarNum: 1,
      //加入购物车的数量
      actId: "",
      //选择规格后确切的id
    });

    getGoodDetail({ id }).then((res) => {
      console.log(res);
      data.basicInfo = res.data.basicInfo;
      data.bannerList = res.data.pics;
      data.content = res.data.content;
      data.properties = res.data.properties;
    });
    //根据id获取商品详情数据并且保存

    const selectHandler = (index, i) => {
      data.selectProperty[index] = i.name;
      //往选择的数组中存东西
      data.selectProIds[index] = i.id;
      //存选中的规格的id

      getActPrice({
        goodsId: data.basicInfo.id,
        propertyChildIds: data.selectProIds.join(","),
        //商品规格要求传的参数是一个字符串 用，拼接
      }).then((res) => {
        console.log(res);
        data.actPrice = res.data.price;
        data.actId = res.data.id;
        //保存确切的价格和确切的id
      });
      //根据 选择的规格请求商品的确切价格
    };
    //选择 规格触发的点击事件

    const addShopcar = () => {
      let obj = {
        img: data.basicInfo.pic,
        name: data.basicInfo.name,
        properties: data.selectProperty.join(" "),
        actPrice: data.actPrice,
        num: data.shopcarNum,
        id: data.actId,
      };
      //我们现在没有一个数据是可以使用的加入购物车的数据 所以我们要自己造一个
      // 购物车数据要包含的信息： 图片 名字 规格 确切的价格 数量 确切的id
      store.commit("add", obj);
      //提交到vuex中添加购物车

      data.showPopup = false;
      //隐藏弹框
    };
    //添加购物车方法

    return { id, ...toRefs(data), selectHandler, addShopcar };
  },
};
</script>
<style lang="scss" scoped>
// 这里不能加scoped
// 如果要加的话 需要用到样式穿透 deep 去解决？
::v-deep .content {
  margin-top: 20px;
  font-size: 15px;
  img {
    width: 100% !important;
    height: 100px !important;
  }
  li {
    height: 34px;
    padding: 0 20px 0 10px;
  }
}

.detail-info {
  display: flex;
  flex-direction: column;
  padding: 0 10px;
  box-sizing: border-box;
  h6 {
    font-size: 16px;
    color: #464646;
    margin-top: 21px;
    font-weight: normal;
  }
  > p {
    font-size: 12px;
    color: #b8b8b8;
    margin-top: 12px;
  }
  > div {
    margin-top: 12px;
    display: flex;
    justify-content: space-between;
    > div {
      span:nth-child(1) {
        color: #cc0c0c;
        font-size: 12px;
      }
      span:nth-child(2) {
        font-size: 12px;
        color: #b8b8b8;
      }
    }
    > span {
      font-size: 12px;
      color: #b8b8b8;
    }
  }
}

.detail-nav {
  display: flex;
  height: 43px;
  font-size: 16px;
  color: #333;
  li {
    flex: 1;
    border-bottom: 1px solid #fff;
  }
  .active {
    border-bottom: 1px solid #ff0000;
  }
}
.detail-footer {
  font-size: 16px;
  width: 100%;
  position: fixed;
  left: 0;
  bottom: 0;
  height: 50px;
  display: flex;
  background: #fff;
  > div {
    display: flex;
    justify-content: center;
    align-items: center;
    width: 50px;
    border-right: 0.01rem solid #e9e9e9;
    border-top: 0.01rem solid #e9e9e9;
    box-sizing: border-box;
  }
  > p {
    display: flex;
    justify-content: center;
    align-items: center;

    flex: 1;
    border-right: 0.01rem solid #e9e9e9;
    border-top: 0.01rem solid #e9e9e9;
  }
  > p:nth-child(5) {
    color: #fff;
    background: #cc0c0c;
  }
}

.pop-pro {
  padding: 20px 0 5px;
  border: 1px solid #e9e9e9;
  color: #333;
  font-size: 12px;
  h5 {
    margin-left: 15px;
  }
  div {
    margin: 0px 0 15px 15px;
    display: flex;
    flex-wrap: wrap;
    span {
      // width: 55px;
      height: 32px;
      border: 1px solid #d9d9d9;
      margin-right: 10px;
      border-radius: 3px;
      padding: 0 10px;
      line-height: 32px;
      margin-top: 10px;
    }
    .active {
      border: 1px solid #b6272d;
    }
  }
}
.popup {
  .sel-num {
    display: flex;
    width: 100%;
    height: 70px;
    padding: 0 20px;
    box-sizing: border-box;
    justify-content: space-between;
    align-items: center;
  }
  .add-shopcar {
    width: 100%;
    height: 50px;
    background: #b6272d;
    color: #fff;
  }
}
</style>
